/**
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to marcus@lateralminds.com.au so I can send you a copy immediately.
 *
 * @copyright  Copyright (c) 2008 Lateral Minds (http://www.lateralminds.com.au)
 * @version    $Id$
 * @license    New BSD License
 */

package com.lateralminds.alfresco.caas.actions;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.UserTransaction;

import org.alfresco.repo.action.executer.ActionExecuterAbstractBase;
import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ParameterDefinition;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.transaction.TransactionService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.lateralminds.alfresco.caas.CaasContentModel;
import com.lateralminds.alfresco.caas.services.KeywordExtractor;

/**
 * An action that uses a keyword extractor to extract keywords from a target
 * document and bind those keywords back against the document
 * 
 * @author Marcus Nyeholt
 *
 */
public class KeywordExtractorActionExecuter extends ActionExecuterAbstractBase
{
    private static Log logger = LogFactory.getLog(KeywordExtractorActionExecuter.class);
    
    public static final String NAME = "KeywordExtractorAction";

    @Override
    protected void executeImpl(Action action, NodeRef target)
    {
        UserTransaction tx = transactionService.getUserTransaction();
        try {
            tx.begin();
            List<String> keywords = this.keywordExtractor.getKeywords(target);
            if (!nodeService.hasAspect(target, CaasContentModel.ASPECT_TAGGABLE)) {
                nodeService.addAspect(target, CaasContentModel.ASPECT_TAGGABLE, null);
            }
            nodeService.setProperty(target, CaasContentModel.PROP_TAGS, (ArrayList) keywords);
            
            tx.commit();
        } catch (Throwable e) {
            if (tx != null) try { tx.rollback(); } catch (Throwable tre) {}
            logger.error("Failed extracting keywords for "+target);
        }
    }

    @Override
    protected void addParameterDefinitions(List<ParameterDefinition> arg0)
    {
    }
    
    private NodeService nodeService;
    private TransactionService transactionService;
    private KeywordExtractor keywordExtractor;
    
    
    public void setKeywordExtractor(KeywordExtractor keywordExtractor)
    {
        this.keywordExtractor = keywordExtractor;
    }

    public void setNodeService(NodeService nodeService)
    {
        this.nodeService = nodeService;
    }

    public void setTransactionService(TransactionService transactionService)
    {
        this.transactionService = transactionService;
    }

}
